package com.example.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.system.dto.CurUserInvitationInfo;
import com.example.system.entity.Invitation;
import com.example.system.exception.CustomException;
import com.example.system.mapper.InvitationMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.service.IInvitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements IInvitationService {
    @Autowired
    InvitationMapper invitationMapper;

    @Autowired
    UserMapper userMapper;

    public void invite(Integer inviterId, Integer inviteeId) {
        if (inviterId.equals(inviteeId)) {
            throw new CustomException("不可邀请自己");
        }
        if (!userMapper.existsUserById(inviterId)) {
            throw new CustomException("当前用户不存在");
        }
        if (!userMapper.existsUserById(inviteeId)) {
            throw new CustomException("被邀请的家庭成员不存在");
        }
        boolean ifInvited = (
                invitationMapper.existsNotRejectedUserIdPair(inviterId, inviteeId) ||
                        invitationMapper.existsNotRejectedUserIdPair(inviteeId, inviterId)
        );
        boolean ifExit = (
                userMapper.existsFamilyMemberPair(inviterId, inviteeId) &&
                        userMapper.existsFamilyMemberPair(inviterId, inviteeId)
        );
        if (!ifExit && !ifInvited) {
            Invitation invitation = new Invitation();
            invitation.setInviterId(inviterId);
            invitation.setInviteeId(inviteeId);
            invitation.setInvitationStatus("no-response-received");
            invitation.setInvitationTime(LocalDateTime.now());
            invitationMapper.invite(invitation);
        } else if (ifInvited) {
            throw new CustomException("当前用户已发送过邀请或被邀请用户已发送过邀请");
        } else {
            throw new CustomException("当前邀请者和被邀请者已互相成为家庭成员");
        }
    }

    public void accept(Integer inviteeId, Integer inviterId) {
        if (invitationMapper.existsNoResponseReceivedUserIdPair(inviterId, inviteeId)) {
            Invitation curInvitation = invitationMapper.getNoResponseReceivedInvitationByUserIdPair(inviterId, inviteeId);
            curInvitation.setInvitationStatus("accepted");
            invitationMapper.update(curInvitation);
            userMapper.addNewFamilyMember(inviterId, inviteeId);
            userMapper.addNewFamilyMember(inviteeId, inviterId);
        } else {
            throw new CustomException("未查询到邀请信息");
        }
    }

    public void reject(Integer inviteeId, Integer inviterId) {
        if (invitationMapper.existsNoResponseReceivedUserIdPair(inviterId, inviteeId)) {
            Invitation curInvitation = invitationMapper.getNoResponseReceivedInvitationByUserIdPair(inviterId, inviteeId);
            curInvitation.setInvitationStatus("rejected");
            invitationMapper.update(curInvitation);
        } else {
            throw new CustomException("未查询到邀请信息");
        }
    }

    public List<CurUserInvitationInfo> getAllInviterRecordsByUserId(Integer userId) {
        if (!userMapper.existsUserById(userId)) {
            throw new CustomException("当前用户不存在");
        }
        return invitationMapper.getAllInviterRecordsByUserId(userId);
    }

    public List<CurUserInvitationInfo> getAllInviteeRecordsByUserId(Integer userId) {
        if (!userMapper.existsUserById(userId)) {
            throw new CustomException("当前用户不存在");
        }
        return invitationMapper.getAllInviteeRecordsByUserId(userId);
    }

    public Integer getAllNewInviteeRecordsCountByUserId(Integer userId) {
        return invitationMapper.getAllNewInviteeRecordsCountByUserId(userId);
    }

    public void deleteOneRecordByInvitationId(Integer invitationId) {
        if (!invitationMapper.existsId(invitationId)) {
            throw new CustomException("当前记录不存在");
        }
        invitationMapper.deleteOneRecordByInvitationId(invitationId);
    }

    public List<CurUserInvitationInfo> getAllInviterRecordsByUserIdAndInviteeId(Integer userId, Integer inviteeId) {
        if (!userMapper.existsUserById(userId)) {
            throw new CustomException("当前用户不存在");
        }
        return invitationMapper.getAllInviterRecordsByUserIdAndInviteeId(userId, inviteeId);
    }

    public List<CurUserInvitationInfo> getAllInviteeRecordsByUserIdAndInviterId(Integer userId, Integer inviterId) {
        if (!userMapper.existsUserById(userId)) {
            throw new CustomException("当前用户不存在");
        }
        return invitationMapper.getAllInviteeRecordsByUserIdAndInviterId(userId, inviterId);
    }
}
